package cn.org.dian.easycommunicate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.SimpleAdapter.ViewBinder;
import cn.org.dian.easycommunicate.model.DataCenter;
import cn.org.dian.easycommunicate.model.Term;
import cn.org.dian.easycommunicate.util.Constants;
import cn.org.dian.easycommunicate.util.Utilities;

/**
 * Display a specific term with all supported languages
 * 
 * @author LiuGen
 * @date 2011-1-21
 */
public class TermActivity extends Activity {

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
                R.layout.progress);
        setContentView(R.layout.term_activity);

        Intent passedIntent = getIntent();
        String termName = passedIntent.getStringExtra("term_name");
        Term term = DataCenter.getTerm(termName);
        setTitle(term.getName());
        prepareData(term);
    }

    private void prepareData(Term term) {
        List<HashMap<String, Object>> contentList = new ArrayList<HashMap<String, Object>>();
        HashMap<String, String> allLanguages = term.getAllLanguages();

        for (String key : allLanguages.keySet()) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("text_foreign", allLanguages.get(key));
            map.put("language", key);

            contentList.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(TermActivity.this,
                contentList, R.layout.term_activity_list_item, new String[] {
                        "text_foreign", "button_speak", "language" },
                new int[] { R.id.text_foreign_2, R.id.button_speak_2,
                        R.id.language_text });
        ListView lv = (ListView) findViewById(R.id.term_content_list);

        // customize a ViewBinder in order to add onClickListener for each
        // ImageButton
        adapter.setViewBinder(new ViewBinder() {

            @Override
            public boolean setViewValue(View view, Object data,
                    String textRepresentation) {
                int viewId = view.getId();
                if (viewId == R.id.text_foreign_2
                        || viewId == R.id.language_text) {
                    // for TextView, do the normal action
                    ((TextView) view).setText((String) data);
                    return true;
                } else if (viewId == R.id.button_speak_2) {
                    // add onClickListener for each ImageButton
                    ((ImageButton) view)
                            .setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // get foreign text for speak
                                    TextView foreignTextView = (TextView) ((LinearLayout) ((RelativeLayout) v
                                            .getParent()).getChildAt(1))
                                            .getChildAt(1);
                                    TextView languageTextView = (TextView) ((LinearLayout) ((RelativeLayout) v
                                            .getParent()).getChildAt(1))
                                            .getChildAt(0);
                                    int languageIndicator = Term
                                            .getAllLanguageNames().indexOf(
                                                    (String) languageTextView
                                                            .getText());
                                    Utilities.playLocalFile(
                                            Constants
                                                    .languageIndicatorToEnum(languageIndicator),
                                            (String) foreignTextView.getText());

                                    // asyncSpeak(languageIndicator, (String)
                                    // foreignTextView.getText());
                                }

                            });
                    return true;
                }
                return false;

            }

        });
        lv.setAdapter(adapter);

    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return Utilities.onCreateOptionsMenu(this, menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return Utilities.onOptionsItemSelected(this, item);
    }
}
